package com.lxk.controller;

import com.lxk.pojo.Goods;
import com.lxk.pojo.Shop;
import com.lxk.service.GoodsService;
import com.lxk.service.OrdersService;
import com.lxk.service.ShopService;
import com.lxk.vo.SalesModel;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author JohnSoybean
 * @create 2020/4/14 10:41
 * @description 销售额统计控制器
 * 本年 本月 本周 本日 筛选时间段
 */
@Controller
public class SalesController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrdersService os;
    @Autowired
    private GoodsService gs;
    @Autowired
    private ShopService ss;

    /**
     * 时间类型转换
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    /**
     * 获取商品销售额统计
     * @return
     */
    @GetMapping("/sales/goods")
    @ResponseBody
    public Result<SalesModel> getGoodsSalesData(Integer goodsId) {
        Date end = new Date();
        Date beginYear = os.getBeginTime(1);
        Date beginMonth = os.getBeginTime(2);
        Date beginWeek = os.getBeginTime(3);
        Date beginDay = os.getBeginTime(4);
        BigDecimal yearSales = os.getGoodsSales(goodsId, beginYear, end);
        BigDecimal monthSales = os.getGoodsSales(goodsId, beginMonth, end);
        BigDecimal weekSales =  os.getGoodsSales(goodsId, beginWeek, end);
        BigDecimal daySales = os.getGoodsSales(goodsId, beginDay, end);
        SalesModel model = new SalesModel();
        model.setYearSale(yearSales);
        model.setMonthSale(monthSales);
        model.setWeekSale(weekSales);
        model.setDaySale(daySales);
        Goods goods = gs.selById(goodsId);
        model.setId(goodsId);
        model.setName(goods.getGoodsName());
        return Result.success(model);
    }


    /**
     * 按时间段查询商品销售额
     * @return
     */
    @PostMapping("/sales/goods/search")
    @ResponseBody
    public Result<BigDecimal> searchGoodsSales(Integer goodsId, Date begin, Date end) {
        BigDecimal goodsSales = os.getGoodsSales(goodsId, begin, end);
        return Result.success(goodsSales);
    }


    /**
     * 获取商品销售额统计
     * @return
     */
    @GetMapping("/sales/shop")
    @ResponseBody
    public Result<SalesModel> getShopSalesData(Integer shopId) {
        Date end = new Date();
        Date beginYear = os.getBeginTime(1);
        Date beginMonth = os.getBeginTime(2);
        Date beginWeek = os.getBeginTime(3);
        Date beginDay = os.getBeginTime(4);
        BigDecimal yearSales = os.getShopSales(shopId, beginYear, end);
        BigDecimal monthSales = os.getShopSales(shopId, beginMonth, end);
        BigDecimal weekSales =  os.getShopSales(shopId, beginWeek, end);
        BigDecimal daySales = os.getShopSales(shopId, beginDay, end);
        SalesModel model = new SalesModel();
        model.setYearSale(yearSales);
        model.setMonthSale(monthSales);
        model.setWeekSale(weekSales);
        model.setDaySale(daySales);
        Shop shop = ss.selShopById(shopId);
        model.setId(shopId);
        model.setName(shop.getShopName());
        return Result.success(model);
    }


    /**
     * 按时间段查询商品销售额
     * @return
     */
    @PostMapping("/sales/shop/search")
    @ResponseBody
    public Result<BigDecimal> searchShopSales(Integer shopId, Date begin, Date end) {
        BigDecimal shopSales = os.getShopSales(shopId, begin, end);
        return Result.success(shopSales);
    }

}
